package com.lenovo.smartengine.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.dolby.api.DsGlobalEx;
import com.lenovo.smartengine.utils.AudioTypeSetUtils;
import com.lenovo.smartengine.utils.Constants;

public class IncomingAudioTypeReceiverEngine extends BroadcastReceiver {
    private DsGlobalEx mDsClient = null;

    public IncomingAudioTypeReceiverEngine(DsGlobalEx mDsClient) {
        this.mDsClient = mDsClient;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        AudioTypeSetUtils.Logi(Constants.TAG, "onReceive...");
        int type_value = 0;
        int phonemediatype = -1;
        int dolby_type_value = -1;
        long broadcast_start = intent.getLongExtra("broadcast_start", 0);
        long broadcast_received = System.currentTimeMillis();
        AudioTypeSetUtils.Logi(Constants.TAG,
                "broadcast received... time consumed:"
                        + (broadcast_received - broadcast_start) + "ms");

        // receive the broadcasting message of media_type
        type_value = intent.getIntExtra(Constants.TYPE_VALUE,
                Constants.ERROR_TYPE);
        AudioTypeSetUtils.Logi(Constants.TAG, "media_type = " + type_value);

        if ((mDsClient != null) && AudioTypeSetUtils.isDolbyApp(mDsClient)
                && AudioTypeSetUtils.isWavesApp(context)) {
            AudioTypeSetUtils
                    .Logi(Constants.TAG,
                            "The Phone has Waves and Dolby at same time,please confirm...");
        } else if (AudioTypeSetUtils.isWavesApp(context)) {
            AudioTypeSetUtils.Logi(Constants.TAG, "isWavesApp...");
            phonemediatype = AudioTypeSetUtils.queryAudioTypeFromWaves(context);
            if (phonemediatype >= Constants.BASE_TYPE) {
                AudioTypeSetUtils.smartEngineToSwitchMediaTypeForWaves(context,
                        phonemediatype, type_value, broadcast_received,
                        broadcast_start);
                AudioTypeSetUtils.Logi(Constants.TAG, "type_value = "
                        + type_value + ", phonemediatype =" + phonemediatype);
            }
        } else if (mDsClient != null && AudioTypeSetUtils.isDolbyApp(mDsClient)) {
            AudioTypeSetUtils.Logi(Constants.TAG, "isDolbyApp...");
            // change the type_value to Dolby media type value
            if (type_value == Constants.WAVES_AUDIO_TYPE) {
                dolby_type_value = Constants.DOLBY_AUDIO_TYPE;
            } else if (type_value == Constants.WAVES_VIDEO_TYPE) {
                dolby_type_value = Constants.DOLBY_VIDEO_TYPE;
            } else {
                dolby_type_value = type_value;
            }
            phonemediatype = AudioTypeSetUtils
                    .queryAudioTypeFromDolby(mDsClient);
            if (phonemediatype >= Constants.BASE_TYPE) {
                AudioTypeSetUtils.smartEngineToSwitchMediaTypeForDolby(context,
                        mDsClient, phonemediatype, dolby_type_value,
                        broadcast_received, broadcast_start);
                AudioTypeSetUtils.Logi(Constants.TAG, "type_value = "
                        + dolby_type_value + ", phonemediatype ="
                        + phonemediatype);
            } else {
                AudioTypeSetUtils.Logi(Constants.TAG, "Dolby is off...");
            }
        } else {
            AudioTypeSetUtils.Logi(Constants.TAG,
                    "The Phone has no Waves or Dolby...");
        }
    }

}
